package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.CompanyEntity;
import com.cn.demo4.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class CompanyController {
    @Autowired
    private ICompanyService iCompanyService;
    @RequestMapping("/admin/company/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, CompanyEntity companyEntity, HttpServletRequest req) throws IOException{
        Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        companyEntity.setStatus(Tool.stringToInt(status));
        List<CompanyEntity> list = iCompanyService.findAll(companyEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/company/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp,CompanyEntity companyEntity, HttpServletRequest req) throws IOException{
        Tool.setEncoding(req,resp);
        CompanyEntity model = iCompanyService.findById(companyEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/company/update")
    @ResponseBody
    public void update(HttpServletResponse resp,CompanyEntity companyEntity,HttpServletRequest req) throws IOException{
        Tool.setEncoding(req,resp);
        //文件流FormData
        LoadImg.loadImg(req);
        companyEntity.setId(Tool.stringToInt((String) req.getAttribute("id")));
        companyEntity.setName((String) req.getAttribute("name"));
        companyEntity.setEmail((String) req.getAttribute("email"));
        companyEntity.setPhone((String) req.getAttribute("phone"));
        companyEntity.setOnline((String) req.getAttribute("online"));
        companyEntity.setAddress((String) req.getAttribute("address"));
        companyEntity.setAddressImg((String) req.getAttribute("addressImg"));
        companyEntity.setContent((String) req.getAttribute("content"));
        companyEntity.setLogo((String) req.getAttribute("img"));
        companyEntity.setQrCodeIndex((String) req.getAttribute("qrCodeIndex"));
        companyEntity.setQrCodeWeiXin((String) req.getAttribute("qrCodeWeiXin"));
        companyEntity.setQrCodeIndexTitle((String) req.getAttribute("qrCodeIndexTiTle"));
        companyEntity.setQrCodeWeiXinTitle((String) req.getAttribute("qrCodeWeiXinTitle"));
        companyEntity.setRecordNumber((String) req.getAttribute("recordNumber"));
        companyEntity.setRecordMessage((String) req.getAttribute("recordMessage"));
        companyEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        companyEntity.setPhoneImg((String) req.getAttribute("phoneImg"));
        String result = iCompanyService.update(companyEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/company/add")
    @ResponseBody
    public void add(HttpServletResponse resp,CompanyEntity companyEntity, HttpServletRequest req) throws IOException{
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        companyEntity.setName((String) req.getAttribute("name"));
        companyEntity.setEmail((String) req.getAttribute("email"));
        companyEntity.setPhone((String) req.getAttribute("phone"));
        companyEntity.setOnline((String) req.getAttribute("online"));
        companyEntity.setAddress((String) req.getAttribute("address"));
        companyEntity.setContent((String) req.getAttribute("content"));
        companyEntity.setLogo((String) req.getAttribute("img"));
        companyEntity.setQrCodeIndex((String) req.getAttribute("qrCodeIndex"));
        companyEntity.setQrCodeWeiXin((String) req.getAttribute("reCodeWeiXin"));
        companyEntity.setQrCodeWeiXinTitle((String) req.getAttribute("qrCodeWeiXinTitle"));
        companyEntity.setQrCodeIndexTitle((String) req.getAttribute("qrCodeIndexTitle"));
        companyEntity.setRecordNumber((String) req.getAttribute("recordNumber"));
        companyEntity.setRecordMessage((String) req.getAttribute("recordMessage"));
        companyEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        String result = iCompanyService.add(companyEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/company/del")
    @ResponseBody
    public void del(HttpServletResponse resp,CompanyEntity companyEntity) throws IOException{
        int result = iCompanyService.del(companyEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
////
//    @RequestMapping("/admin/company/getCompanyModelListForHome")
//    @ResponseBody
//    public void getCompanyModelForHome(HttpServletResponse resp,CompanyEntity companyEntity, HttpServletRequest req) throws IOException{
//        CompanyEntity model = iCompanyService.getCompanyModelForHome(companyEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("model",model);
//        resp.getWriter().println(jsonObject);
//    }
}
